import { ScreepsApi } from 'screeps-simple-api'
import dotenv from 'dotenv'
import { IPlugin } from '../../bot/bot'
import commandHandlers from './commandHandler'
import { Sender } from '../../bot/handler.ts/messageHandler'

dotenv.config()

class ScreepsPlugin implements IPlugin {
    name = 'Screeps插件'
    api: ScreepsApi
    constructor() {
        const { SCREEPS_ACCOUNT, SCREEPS_PASSWORD } = process.env
        if (!SCREEPS_ACCOUNT || !SCREEPS_PASSWORD) throw new Error('请在.env文件中配置SCREEPS账号和密码！')
        this.api = new ScreepsApi({
            email: SCREEPS_ACCOUNT,
            password: SCREEPS_PASSWORD,
        })
    }
    onMessage(sender: Sender) {
        if (!sender.command) return
        if (sender.command.name === '/help') {
            sender.reply('目前支持的命令有：\n' + commandHandlers.map((handler) => handler.commandName).join('\n'))
            return
        }
        for (const handler of commandHandlers) {
            if (sender.command.name === handler.commandName) {
                handler.execute(sender, this.api)
            }
        }
    }
}

export default new ScreepsPlugin()
